{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7be45502",
   "metadata": {},
   "source": [
    "--------\n",
    "\n",
    "* 日期：2022.10.12 / week08\n",
    "* 课程：python\n",
    "* 记录人：凪\n",
    "\n",
    "--------\n",
    "\n",
    "# 本周学习内容\n",
    "\n",
    "> 1. 有序的数据结构-字符串\n",
    ">> 1. 字符串的基本概念（回顾）\n",
    ">>> 1. Single quotes: 'allows embedded \"double\" quotes'\n",
    ">>> 2. Double quotes: \"allows embedded 'single' quotes\"\n",
    ">>> 3. Triple quoted: '''Three single quotes''', \"\"\"Three double quotes\"\"\"\n",
    ">> 2. 字符串的性质和方法\n",
    "\n",
    "> 2. 有序的数据结构-列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33ce13a7",
   "metadata": {},
   "source": [
    "# 1.字符串的性质和方法\n",
    "\n",
    "* 字符串的性质\n",
    "> 1. 字符串的有序性\n",
    "> 2. 字符串是有长度的\n",
    ">> 1. 使用len()检查字符串的长度\n",
    "* 字符串的基础方法(运算法则)\n",
    "> 1. 字符串的取值:str[index]\n",
    "> 2.字符串的切片：str[start,stop]\n",
    "> 3. 字符串的拼接:+\n",
    "> 4. in 检查成员是否存在 #https://docs.python.org/3/library/stdtypes.html#common-sequence-operations\n",
    "> 5.count()检查字符串出现的次数\n",
    "* 字符串的一般方法（funtion/Methods 函数/方法）\n",
    "#https://docs.python.org/3/library/stdtypes.html#string-methods\n",
    "> 举个例子 isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fd93cee4",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"今天好累\" # 每个字都是有顺序的 这几个字相当于\"values\"；暗藏的顺序号码相当于\"index\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "347298ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'今'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2fe5e7c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'累'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "36fca962",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'好'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2f45a339",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [5]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtext\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m99\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "text[99] #字符串是有长度的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ac930502",
   "metadata": {},
   "outputs": [],
   "source": [
    "# index=索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "350dbe27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text) # 1.使用len()检查字符串的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eec9bd86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'今天好累'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(text) # 1.字符串的取值:str[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2acc6158",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'天天累'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[1]+text[1]+text[3] # 3. 字符串的拼接:+"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "90f2111d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'今天好'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0:3] # 等价于 text[:3] 截一下,:是stop的意思 2.字符串的切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "b33da728",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'天好'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3:-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b3ea4bd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'天好累'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3: ] # 如果到0，直接省略打个空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "73fd9d06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"今天\" in text # 4.in检查是否在里面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "10aa2ad9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'今天好累今天好累今天好累'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * text # “*”就是乘于"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cde61cc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * text.count(\"今天\") # 5.count()检查字符串出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "649d8b71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text.count(\"今天\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c50d8eb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串的一般方法（funtion/Methods 函数/方法） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "63adacf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0d6d92bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_1 = 20\n",
    "age_2 = \"20\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4df76065",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "print(age_1)\n",
    "print(age_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "de6a8b41",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [31]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mage_1\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mage_2\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "age_1 + age_2 #不可以相加，因为性质不同。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "132c9f67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20 <class 'int'>\n",
      "20 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(age_1,type(age_1))\n",
    "print(age_2,type(age_2)) # 查询性质"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ee6cff52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串是数值（用isdigit检查），可以用int直接强制转化为真正的数值\n",
    "if age_2.isdigit(): # 查询是否是数值\n",
    "    print(age_1 + int(age_2)) # 转变性质，相加"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2d3c2ee",
   "metadata": {},
   "source": [
    "# 2.有序的数据结构-列表(List)\n",
    "* 1.列表(List)的概念和基本性质\n",
    "> 1. Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.\n",
    "\n",
    "* 2.列表(List)的一般方法( ？如何学习)\n",
    "> 1. 增加\n",
    "> 2. 删除\n",
    "> 3. 改\n",
    "> 4. 查：in\n",
    "\n",
    "* 列表(List)的学习方法(步骤)\n",
    "> 1.使用.Tab键查看列表的方法,每个都要记住\n",
    "> 2. 使用help（查看方法的使用规则）\n",
    "\n",
    "### 2.1 列表的性质\n",
    "\n",
    "* 和字符串的运算法则完全一致（加、乘、切片...)\n",
    "* 注意： 字符串是对自负进行操作，列表是对元素(items)进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "678ee332",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串的01234指的是字符\n",
    "# 列表的01234指的是元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "205ae464",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 7, 66, 36]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表的形式\n",
    "squares = [1, 3, 7, 66, 36] # 列表里放数值\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "256c6985",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Magma', 'sylvia', 'jan', 'ali']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Magma\", \"sylvia\", \"jan\", \"ali\"] # 列表里放字符串\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "35f996c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Magma', 'sylvia', 1, 3]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares = [\"Magma\", \"sylvia\", 1, 3] # 列表里放数字和字符串\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "bc8ab64a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 7, 66, 36, 'Magma', 'sylvia', 'jan', 'ali']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares + names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "2f4c1418",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以试试加、乘、切片..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "df2add50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['sylvia', 'jan']"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "7725c51f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jan', 'ali']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a920d581",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Magma', 'jan']"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出\"Magma\"，“jan” 用切片的方法\n",
    "names[:3:2]  #???"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c88d6b0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "    .ipynb_checkpoints/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "193c991e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  ？如何学习：使用.Tab键查看列表的方法,每个都要记住\n",
    "names."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "0da5481c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法1. 使用.Tab\n",
    "\n",
    "# append-增加。要加（函数的参数）,\n",
    "# clear-\n",
    "# copy-复制\n",
    "# count-\n",
    "# extend\n",
    "# index\n",
    "# insert\n",
    "# pop-增加\n",
    "# remove-移除\n",
    "# reverse\n",
    "# sort-\n",
    "\n",
    "# 方法2.使用help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "7e541e8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Magma', 'sylvia', 'jan', 'ali']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Magma\", \"sylvia\", \"jan\", \"ali\"] # 列表里放字符串\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "9b97d7c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"wooo\") #方法1 .tab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "0505bd3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Magma', 'sylvia', 'jan', 'ali', 'wooo', 'wooo']"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "b783c443",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append) # 方法2 使用help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d094c0d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"no\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "b4a652c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Magma', 'sylvia', 'jan', 'ali', 'wooo', 'wooo', 'no']"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e70f920",
   "metadata": {},
   "source": [
    "# 课后作业：\n",
    "* 列表的方法尝试所有的方法\n",
    "* 总结列表的方法学习心得（表格形式总结） \n",
    "| 列表的方法 | Description |使用方法 | 备注 | | ----------- | ----------- |----------- |----------- | | append | Append object to the end of the list. |list.append(object)|注意：增加在末尾！| | Paragraph | Text |Text |Text |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7797782f",
   "metadata": {},
   "source": [
    "| 列表的方法 | Description |使用方法 | 备注 |\n",
    "| ----------- | ----------- |----------- |----------- |\n",
    "| append | Append object to the end of the list. |list.append(object)|注意：增加在末尾！|\n",
    "| Paragraph | Text |Text |Text |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "a6ae3a5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://www.markdownguide.org/extended-syntax/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eec5e73",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
